/**
 * 实名认证、银行卡相关的js
 */


//认证图片上传
function uploadImage(flag) {
    try {
        var strUrl = "/verified/uploadImgCheck";
        //myApp.showPreloader('正在处理,请稍候');

        //document.domain = "183.134.73.203";
        //myApp.alert(document.domain);
        $.ajaxFileUpload({
            url: strUrl,   //提交的路径
            secureuri: false, // 是否启用安全提交，默认为false
            fileElementId: 'imgsil' + flag, // file控件id
            dataType: 'JSON',
            data: {},
            async: true,
            success: function (data, status) {
                //myApp.alert("success");
                //myApp.hidePreloader();

                //alert("success--status["+status"]");
                //myApp.alert("success["+data"]");

                data = $.parseJSON(data);
                var src = data.src;
                //$(this).css("background-image","url(on.jpg)");
                $('#frontUrl' + flag).val(src);
                try {
                    $('#ver_img' + flag).attr("src", "/resources/images/verified/zh_dn11.png");
                } catch (e) {
                    alert(e.message);
                }
                if ("rz" != flag) {
                    $("#cardmodel" + flag).css("display", "none");
                    $("#frontImg" + flag).attr("src", "/verified/getImg?path=verified/" + src);
                    $('#filimg_div' + flag).attr("style", "position:absolute;z-index:3;left: 14px;top: 9px;background: rgba(0,0,0,0.5);width:130px;height: 86px;border-radius: 5px;");
                } else {
                    var img_style = "width: 120px;height:80px;position:absolute;z-index:4;left:50%;margin-left:-56px;margin-top:15px;border-radius:5px;";
                    $("#frontImg" + flag).attr("src", "/verified/getImg?path=cardauth/" + src);
                    $('#filimg_div' + flag).attr("style", img_style);
                }

                if (data.error == 0) {
                    try {
                        if ("z" == flag) {
                            $("#infoz").css('display', 'block');
                            $("#cardmodel" + flag).css("display", "none");
                            $("#name").val(data.name);
                            $("#idnumber").val(data.idnumber);

                        } else if ("f" == flag) {
                            $("#infof").css('display', 'block');
                            $("#validdate").val(data.validdate);
                            $("#authority").val(data.authority);
                            $("#cardmodel" + flag).css("display", "none");
                        } else if ("y" == flag) {
                            $("#infoy").css('display', 'block');
                            $("#cardNo").val(data.cardno.substr(1, data.cardno.length));
                            $("#bankName").val(data.bankname);
                            $("#cardmodel" + flag).css("display", "none");
                        } else if ("rz" == flag) {
                            $("#cardnoxy").css('display', 'block');
                            $("#banknamexy").css('display', 'block');
                            $("#cardnoxy").val(data.cardno.substr(1, data.cardno.length));
                            $("#banknamexy").val(data.bankname);
                        }
                    } catch (e) {
                        alert(e.message);
                    }
                    alert(data.message);
                } else {

                    if ("y" == flag) {
                        $("#infoy").css('display', 'block');
                        // $("#cardno").css('display', 'block');
                        // $("#bankname").css('display', 'block');
                    } else if ("z" == flag) {
                        $("#infoz").css('display', 'block');
                        // $("#name").css('display', 'block');
                        // $("#idnumber").css('display', 'block');
                    } else if ("f" == flag) {
                        $("#infof").css('display', 'block');
                        // $("#validdate").css('display', 'block');
                        // $("#authority").css('display', 'block');
                    } else if ("rz" == flag) {
                        $("#cardnoxy").css('display', 'block');
                        $("#banknamexy").css('display', 'block');
                    }
                    alert(data.message);
                }
            },
            error: function (data, status, e) {
                alert("error[" + e.message + "]" + status);
                alert("error:" + data.responseText);
                if ("z" == flag) {
                    $("#cardmodel" + flag).css("display", "none");
                    $("#name").css('display', 'block');
                    $("#idnumber").css('display', 'block');
                } else if ("f" == flag) {
                    $("#cardmodel" + flag).css("display", "none");
                    $("#validdate").css('display', 'block');
                    $("#authority").css('display', 'block');
                } else if ("y" == flag) {
                    $("#cardmodel" + flag).css("display", "none");
                    $("#cardno").css('display', 'block');
                    $("#bankname").css('display', 'block');
                    $('#frontUrl' + flag).val("frontUrly_test");
                } else if ("rz" == flag) {
                    $("#cardnoxy").css('display', 'block');
                    $("#banknamexy").css('display', 'block');
                    $('#frontUrl' + flag).val("frontUrly_test");
                }

                //myApp.hidePreloader();
                alert("照片上传失败,请输入相关信息");
            }
        });

    } catch (e) {
        alert(e.message);
    }
}

function verified_commit() {

    var verified_btn = $("#verified_btn");
    //设置按钮不可点击
    verified_btn.addClass('disabled');

    var name = $("#name").val();
    var idnumber = $("#idnumber").val();
    var validdate = $("#validdate").val();
    var frontUrlz = $('#frontUrlz').val();
    var frontUrlf = $('#frontUrlf').val();
    var authority = $("#authority").val();
    //验证非空
    if (frontUrlz == '') {
        layer.msg("请上传身份证正面照片", {
                shift: 6
                , shade: 0.3
                , time: 2500
                , offset: "165px"
                , closeBtn: 1
                , shadeClose: true
            }, function () {
            }
        );

        verified_btn.removeClass('disabled');
        return false;
    }

    if ($('#frontUrlf').val() == '') {
        layer.msg("请上传身份证反面照片", {
                shift: 6
                , shade: 0.3
                , time: 2500
                , offset: "165px"
                , closeBtn: 1
                , shadeClose: true
            }, function () {
            }
        );
        verified_btn.removeClass('disabled');
        return false;
    }

    if ('' == name || '' == idnumber) {
        layer.msg("请填写姓名和身份证号信息", {
                shift: 6
                , shade: 0.3
                , time: 2500
                , offset: "165px"
                , closeBtn: 1
                , shadeClose: true
            }, function () {
            }
        );
        verified_btn.removeClass('disabled');
        return false;
    }

    // if ('' == cardno || '' == bankname) {
    //     layer.msg("请填写结算卡信息", {
    //             shift: 6
    //             , shade: 0.3
    //             , time: 2500
    //             , offset: "165px"
    //             , closeBtn: 1
    //             , shadeClose: true
    //         }, function () {
    //         }
    //     );
    //     verified_btn.removeClass('disabled');
    //     return false;
    // }
    if ('' == authority) {
        layer.msg("请填写签发机关", {
                shift: 6
                , shade: 0.3
                , time: 2500
                , offset: "165px"
                , closeBtn: 1
                , shadeClose: true
            }, function () {
            }
        );
        verified_btn.removeClass('disabled');
        return false;
    }

    if ('' == validdate) {
        layer.msg("请填写身份证有效期", {
                shift: 6
                , shade: 0.3
                , time: 2500
                , offset: "165px"
                , closeBtn: 1
                , shadeClose: true
            }, function () {
            }
        );
        verified_btn.removeClass('disabled');
        return false;
    }


    // if ('' == bankname) {
    //     layer.msg("请填写结算卡银行名称", {
    //             shift: 6
    //             , shade: 0.3
    //             , time: 2500
    //             , offset: "165px"
    //             , closeBtn: 1
    //             , shadeClose: true
    //         }, function () {
    //         }
    //     );
    //     verified_btn.removeClass('disabled');
    //     return false;
    // }
    // if ('' == province_id) {
    //     layer.msg("请选择开户行省份", {
    //             shift: 6
    //             , shade: 0.3
    //             , time: 2500
    //             , offset: "165px"
    //             , closeBtn: 1
    //             , shadeClose: true
    //         }, function () {
    //         }
    //     );
    //     verified_btn.removeClass('disabled');
    //     return false;
    // }
    // if ('' == city_id) {
    //     layer.msg("请选择开户行城市", {
    //             shift: 6
    //             , shade: 0.3
    //             , time: 2500
    //             , offset: "165px"
    //             , closeBtn: 1
    //             , shadeClose: true
    //         }, function () {
    //         }
    //     );
    //     verified_btn.removeClass('disabled');
    //     return false;
    // }

    //myApp.showPreloader('正在处理,请稍候');

    $.ajax({
        type: "POST",
        url: "/verified/verified",
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        dataType: "json",
        data: {
            idnumber: idnumber,
            validdate: validdate,
            name: name,
            idcard_authority: authority,
            frontUrlz: frontUrlz,
            frontUrlf: frontUrlf
        },
        success: function (data) {
            //myApp.hidePreloader();
            if (data.code != 0) {
                alert(data.message);
                return;
            } else {
                //跳转为认证成功页面
                alert("认证成功");
                location.href = "/payCenter";
            }
        }
    });
}

function bindSettleCard() {
    var bindBtn = $("#bind_btn");
    bindBtn.addClass('disabled');
    var cardNo = $("#cardNo").val();
    var bankName = $("#bankName").val();
    var settlePhone = $("#settlePhone").val();

    //验证非空
    if ($('#frontUrly').val() == '') {
        alert("请上传银行卡正面照片");
        bindBtn.removeClass('disabled');
        return;
    }


    if (bankName == '') {
        alert("银行名称不能为空");
        bindBtn.removeClass('disabled');
        return;
    }

    if (cardNo == '') {
        alert("卡号不能为空");
        bindBtn.removeClass('disabled');
        return;
    }

    if (settlePhone == '') {
        alert("预留手机号不能为空");
        bindBtn.removeClass('disabled');
        return;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
    if(!myreg.test(settlePhone)){
        alert("请输入正确的手机号");
        bindBtn.removeClass('disabled');
        return;
    }

    $.ajax({
        type: "POST",
        url: "/trans/bindSettlePan",
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        dataType: "json",
        data: {
            cardNo: cardNo,
            bankName: bankName,
            settlePhone: settlePhone,
            frontUrly: $('#frontUrly').val()
        },
        success: function (data) {

            if (data.code != 0) {
                alert(data.message);
                bindBtn.removeClass('disabled');
                return;
            } else {
                location.href="/trans/toCode";
            }
        },
        error: function (data, status) {
            alert("银行卡添加失败");
            bindBtn.removeClass('disabled');
            return;
        }
    })
}
function confirmBind() {
    var codeBtn = $("#code_btn");
    codeBtn.addClass('disabled');
    var verifyCode = $("#verifyCode").val();
    if (verifyCode == '') {
        alert("验证码不能为空");
        codeBtn.removeClass('disabled');
        return;
    }
    $.ajax({
        type: "POST",
        url: "/trans/confirmBind",
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        dataType: "json",
        data: {
            verifyCode: verifyCode
        },
        success: function (data) {
            //myApp.hidePreloader();
            if (data.code != 0) {
                alert(data.message);
                codeBtn.removeClass('disabled');
                return;
            } else {
                //跳转为认证成功页面
                alert("添加成功");
                location.href = "/payCenter";
            }
        }
    });
}
